package org.summerframework.core.validation;


import org.summerframework.core.util.CheckUtil;
import org.summerframework.core.util.IdCardUtil;
import org.summerframework.core.validation.constraints.IdCardCheck;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author liaorongbiao
 * @version 1.0
 * @date 2019-06-26 09:31
 **/
public class IdCardCheckValidate implements ConstraintValidator<IdCardCheck, String> {

    private IdCardCheck idCardCheck;


    @Override
    public void initialize(IdCardCheck constraintAnnotation) {
        this.idCardCheck = constraintAnnotation;
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        
        return idCardCheck.strict() ? IdCardUtil.validatedIdCard(value) : CheckUtil.checkReg(CheckUtil.IDENTITY_CARD, value);
    }
}
